package com.quasar.hpatient.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.DayItemRecord;
import com.quasar.hpatient.model.litePalmodel.DailyItemInfodb;
import com.quasar.hpatient.presenter.DailyRecordPresenter;
import com.quasar.hpatient.utils.LogAndToastUtil;
import com.quasar.hpatient.utils.OutliersValues;
import com.quasar.hpatient.view.DailyLinActivity_;
import com.vise.log.ViseLog;
import java.util.List;

/**
 * 日常记录更多
 * Created by Administrator on 2017/7/30 0030.
 */

public class Widget_DayRecord_Item extends LinearLayout implements View.OnClickListener, WidgetNumber.OnSave {

    private DailyRecordPresenter dailyRecordPresenter;

    @Override
    public void save(WidgetNumber item) {
        getItem();
    }

    private List<DailyItemInfodb> dailyItemInfoDBList;
    DailyItemInfodb dailyItemInfodb;
    private Context context;
    //传递时间
    private String testingDate;
    private ImageView trash;//删除

    public String getTestingDate() {
        return testingDate;
    }

    public void setTestingDate(String testingDate) {
        this.testingDate = testingDate;
    }

    public Widget_DayRecord_Item(final Context context) {
        super(context);

        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.widget_three_edit, this);
        tv_title = (TextView) findViewById(R.id.tv_title);
        num = (EditText) findViewById(R.id.tv_values);
        ll_layout = (LinearLayout) findViewById(R.id.ll_layout);
        //检测值
        tv_reference = (TextView) findViewById(R.id.tv_reference);
        tv_title.setOnClickListener(this);
        num.setOnClickListener(this);
        dailyItemInfoDBList = MainAppliaction.getInstance().getDailyItemInfoList();

        num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {



                if (OutliersValues.editTextNum(num.getText().toString().trim())) {


                    if (dayItemRecord.getDailyitemName().equals(ConstantManger.OUTSUM) ||
                            dayItemRecord.getDailyitemName().equals(ConstantManger.INSUM) ||
                            dayItemRecord.getDailyitemName().equals(ConstantManger.night) ||
                            dayItemRecord.getDailyitemName().equals(ConstantManger.BMI)) {
                        textCore(0);
                    } else {
                        if (!dayItemRecord.getDailyitemName().equals(ConstantManger.BMI)) {
                            //如果为心率检测数值是否为整数
                            if(dayItemRecord.getDailyitemName().equals(ConstantManger.HEARTRATENAME)){
                                if(s.toString().matches("^[0-9]*[1-9][0-9]*$")){//判断正整数
                                    textColer(num.getText().toString());
                                }else{
                                    LogAndToastUtil.toast(context,"心率不能为小数");
                                    num.setText("");
                                }
                            }else{
                                textColer(num.getText().toString());
                            }
                        }
                    }
                }

            }
        });
        trash = (ImageView) findViewById(R.id.trash);
        trash.setOnClickListener(this);
    }

    private int color(int value) {
        return MainAppliaction.getInstance().getResources().getColor(value);
    }

    private TextView tv_title;
    EditText num;
    //检测值
    TextView tv_reference;
    public DayItemRecord dayItemRecord;

    public DayItemRecord getItem() {

        DayItemRecord Record = new DayItemRecord();
        if (Record.getLogValue() != null) {

            if (!Record.getLogValue().equals(num.getText().toString())) {
                Record.setStats(1);
            } else {
                Record.setStats(0);
            }
        }

        if (num.getText().length() == 0) {
            Record.setLogValue("");
        } else {
            //判断是血压的时候不从界面上取值
            if (dayItemRecord.getDailyitemName().equals(ConstantManger.BLOODPRESSURENAME)) {
                Record.setLogValue(dayItemRecord.getLogValue());
                Record.setPaired(dayItemRecord.getPaired());
            } else {
                Record.setLogValue(num.getText().toString());
            }
        }
        Record.setDailyitemName(tv_title.getText().toString().trim()); //名称
        Record.setItemId(dayItemRecord.getItemId());
        Record.setDailyitemDose(dayItemRecord.getDailyitemDose());
        Record.setId(dayItemRecord.getId());
        Record.setMarkValue(dayItemRecord.getMarkValue());
        Record.setReference(dayItemRecord.getReference());
        Record.setLogType(dayItemRecord.getLogType());

        return Record;
    }

    //传递值
    public void setItem(DayItemRecord dayItemRecord) {
        this.dayItemRecord = dayItemRecord;

        if (dayItemRecord.getDailyitemName().equals(ConstantManger.BLOODPRESSURENAME)
                ) {
            num.setFocusable(false);
            num.setFocusableInTouchMode(false);
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(dayItemRecord.getDailyitemName());
            tv_title.setText(stringBuffer.toString());
        } else {
            tv_title.setText(dayItemRecord.getDailyitemName());
        }


        //bmi,总出量，总入量不能修改
        if (dayItemRecord.getDailyitemName().equals(ConstantManger.BMI) ||
                dayItemRecord.getDailyitemName().equals(ConstantManger.OUTSUM) ||
                dayItemRecord.getDailyitemName().equals(ConstantManger.INSUM) ||
                dayItemRecord.getDailyitemName().equals(ConstantManger.night) ||
                dayItemRecord.getDailyitemName().equals(ConstantManger.height) ||
                dayItemRecord.getDailyitemName().equals(ConstantManger.weight)||
                dayItemRecord.getDailyitemName().equals(ConstantManger.TEMPERATURENAME)||
                dayItemRecord.getDailyitemName().equals(ConstantManger.BLOODPRESSURENAME) ||
                dayItemRecord.getDailyitemName().equals(ConstantManger.HEARTRATENAME) ||
                dayItemRecord.getDailyitemName().equals(ConstantManger.BILLDSUGAR)
                ) {
            TextPaint paint = tv_title.getPaint();
            paint.setFakeBoldText(true);
            num.setFocusable(false);
            num.setFocusableInTouchMode(false);
        }

        for (int i = 0; i < dailyItemInfoDBList.size(); i++) {
            if (dailyItemInfoDBList.get(i).getPrimaryid() == Integer.parseInt(dayItemRecord.getItemId())) {
                dailyItemInfodb = dailyItemInfoDBList.get(i);
                break;
            }
        }

        if (dayItemRecord.getLogValue() != null) {
            if (dayItemRecord.getLogValue().equals("0")) {
                num.setText("");
            } else {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(dayItemRecord.getLogValue());
                if (dayItemRecord.getDailyitemName().equals(ConstantManger.BLOODPRESSURENAME)) {
                    if (dayItemRecord.getPaired() != null) {
                        stringBuffer.append("/" + dayItemRecord.getPaired());
                    }
                }
                num.setText(stringBuffer.toString());
                if (!dayItemRecord.getDailyitemName().equals(ConstantManger.BMI)) {

                    if(!dayItemRecord.getDailyitemName().equals(ConstantManger.BLOODITEMLIST)){
                        textColer(dayItemRecord.getLogValue());
                    }else{
                        boolVaiue(num.getText().toString());
                    }
                }
            }
        } else {
            num.setText("");
        }

        if(!dayItemRecord.getDailyitemName().equals(ConstantManger.BMI)){
            tv_reference.setText(dayItemRecord.getDailyitemDose());//单位
        }
        //判断是否隐藏
        if(dayItemRecord.getMarkValue()!=null){
            if(dayItemRecord.getMarkValue().equals("删除")){
                hideView();
            }else{
                ll_layout.setVisibility(View.VISIBLE);
            }
        }else{
            ll_layout.setVisibility(View.VISIBLE);
        }

    }

    private void textCore(int textColor) {
        switch (textColor) {
            //正常值
            case OutliersValues.NORMALVALUE://0
                num.setTextColor(color(R.color.btcolor));
                break;

            //大于最大值
            case OutliersValues.UPNORMALVALUE://1
                num.setTextColor(color(R.color.values_err));
                break;

            case OutliersValues.DANGERLVALUE://2
                num.setTextColor(color(R.color.values_err));
                break;

            //小于最小值
            case OutliersValues.LOWNORMALVALUE://-1
                num.setTextColor(color(R.color.values_err));
                break;
            case OutliersValues.BOUNDARYVALUE:
                ViseLog.d("BOUNDARYVALUE" + OutliersValues.BOUNDARYVALUE);
                // msg.setError("请核对数据");
                break;

        }
    }

    //值改变字体颜色改变
    public void textColer(String val) {

        if (OutliersValues.editTextNum(val)) {

            float floValua = Float.parseFloat(val);

            if (dayItemRecord.getDailyitemName().equals(ConstantManger.TEMPERATURENAME)) {//体温
                int textColor = OutliersValues.TiWencrisisVal(floValua);//-1危机体温，0正常,1低热，2高热
                textCore(textColor);
                setAbnormal(textColor);

            } else if (dayItemRecord.getDailyitemName().equals(ConstantManger.HEARTRATENAME)) {//判断心率
                int textColor = OutliersValues.hearRate(floValua);//心动过缓3蓝  心动过速4 0正常
                if (textColor == 3) {
                    textCore(OutliersValues.UPNORMALVALUE);
                } else if (textColor == 4) {
                    textCore(OutliersValues.DANGERLVALUE);
                } else {
                    textCore(OutliersValues.NORMALVALUE);
                }
                setAbnormal(textColor);
            } else if (dayItemRecord.getDailyitemName().equals(ConstantManger.BILLDSUGAR)) {//血糖
                int textColor = OutliersValues.bloodSugar(floValua); //0正常1高血糖2危急血糖
                if (textColor == 1) {
                    textCore(OutliersValues.DANGERLVALUE);
                } else if (textColor == 2) {
                    textCore(OutliersValues.DANGERLVALUE);
                } else {
                    textCore(OutliersValues.NORMALVALUE);
                }
                setAbnormal(textColor);
            }else{
                textEerrVal(val);
                textCore(OutliersValues.NORMALVALUE);
                setAbnormal(OutliersValues.NORMALVALUE);
            }
        } else{
            textEerrVal(val);
            textCore(OutliersValues.NORMALVALUE);
            setAbnormal(OutliersValues.NORMALVALUE);
        }

    }

    public void boolVaiue(String val){
        if (dayItemRecord.getDailyitemName().equals(ConstantManger.BLOODITEMLIST)) {
            String[] valText = val.split("/");

            if (valText.length == 2) {
                int num = OutliersValues.Valstats(valText[0], valText[1]);
                if (num == 2 || num == 3) {
                    textCore(OutliersValues.NORMALVALUE);
                } else {
                    textCore(OutliersValues.DANGERLVALUE);
                }
            }
        }
    }

    //判断数值是否合法
    public void textEerrVal(String val) {

        int satts = 0;
        if (dailyItemInfodb != null) {
            if (dailyItemInfodb.getStandard() != null) {
                int textColor = OutliersValues.TextColor(
                        val,
                        dailyItemInfodb.getStandard());
                satts = textColor;
            }
        }
        //异常值为-1的是都对输入框进行清空
        if (satts ==1) {
            if(dailyItemInfodb.getName().equals(ConstantManger.OUTSUM)
                    || dailyItemInfodb.getName().equals(ConstantManger.INSUM) ){
                ViseLog.d("检测到的出入量"+dailyItemInfodb.getName());
            }else{
                LogAndToastUtil.toast(context,"检测值为不可能值","");
                num.setText("");
            }
        }
    }

    //删除按钮
    public interface OnRemove {
        public void remove(Widget_DayRecord_Item item);

        public void edittextVal(Widget_DayRecord_Item item);

        public void OtherVal(Widget_DayRecord_Item item);
    }

    public OnRemove getRemove() {
        return remove;
    }
    public void setRemove(OnRemove remove) {
        this.remove = remove;
    }
    private OnRemove remove;


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_title) {
            //折线图
            String titleName = tv_title.getText().toString().trim();
            String itemName = "";
            if (titleName.equals(ConstantManger.OUTSUM) || titleName.equals(ConstantManger.INSUM) || titleName.equals(ConstantManger.night)) {
                itemName = ConstantManger.INOUTPUTVAL;
            } else {
                itemName = titleName;
            }

            ViseLog.d("传过来的参数"+getTestingDate());
            DailyLinActivity_.intent(context)
                    .dataTime(getTestingDate()).itemName(itemName).start();
        } else if (v.getId() == R.id.tv_values) {

            //检测血压的变化
//            if (dayItemRecord.getDailyitemName().equals(ConstantManger.BLOODPRESSURENAME)
//                    ) {
//                edittText(dayItemRecord);
//            }else
            if(dayItemRecord.getDailyitemName().equals(ConstantManger.height) || dayItemRecord.getDailyitemName().equals(ConstantManger.weight)){
                if (remove != null) {
                    remove.edittextVal(this);
                }
            }else{
                if (remove != null) {
                    remove.OtherVal(this);
                }
            }
        } else if (v.getId() == R.id.trash) {
            if (remove != null) {
                remove.remove(this);
            }
        }
    }


    //隐藏布局
    LinearLayout ll_layout;

    public void hideView() {
        ll_layout.setVisibility(View.GONE);
    }

    public int Abnormal;//正常

    public int getAbnormal() {
        return Abnormal;
    }

    public void setAbnormal(int abnormal) {
        Abnormal = abnormal;
    }
}
